package com.lagou.mr.speak;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class SpeakMapper extends Mapper<LongWritable, Text, Text, SpeakBean> {

    Text k = new Text();
    SpeakBean v = new SpeakBean();
    // 设备号
    private String deviceId;
    // 自有播放时长
    private long self_duration;
    // 第三方播放时长
    private long third_partDuration;

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] splitStr = StringUtils.split(value.toString(), "    ");
        int len = splitStr.length;
        deviceId = splitStr[1];
        self_duration = Long.parseLong(splitStr[len - 3]);
        third_partDuration = Long.parseLong(splitStr[len - 2]);
        v.set(self_duration, third_partDuration);
        k.set(deviceId);
        context.write(k, v);
    }
}
